from django.db import models
import os
from system.models import Image
from django.conf import settings
# Create your models here.
class Company(models.Model):
    """
    公司
    """
    name = models.CharField(max_length=100, default='',null=False, verbose_name='名称')
    license_number = models.CharField(max_length=100,default='', verbose_name='营业执照编号')
    is_auth = models.PositiveSmallIntegerField(default=0,verbose_name='是否认证')
    id_card = models.CharField(max_length=18, verbose_name='法人身份证号')
    corporation_front_url = models.CharField(max_length=255, default='',verbose_name='法人身份证头像面')
    corporation_back_url = models.CharField(max_length=255,default='', verbose_name='法人身份证国徽面')
    corporation_front_image_id = models.CharField(max_length=18,default='', verbose_name='法人身份证头像面id')
    corporation_back_image_id = models.CharField(max_length=18, default='',verbose_name='法人身份证国徽面id')
    license_url = models.CharField(verbose_name='营业执照',max_length=255,default='', blank=True, null=True)
    mobile = models.CharField(max_length=11, verbose_name='法人手机号')
    corporation = models.CharField(max_length=11,default="", verbose_name='法人姓名')
    image_id = models.CharField(max_length=11,default='',null=True, verbose_name='关联imageId')


    class Meta:
        verbose_name = '公司管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def delete(self, using=None, keep_parents=False):
        files = getattr(self, 'license_url')
        if not files:
            return
        fname = os.path.join(settings.MEDIA_ROOT, str(files))
        if os.path.isfile(fname):
            os.remove(fname)

        try:
            Image.objects.filter(id=self.image_id).delete()
        except Exception as e:
            pass
        super().delete()


class Department(models.Model):
    """
    部门
    """
    company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name='部门')
    name = models.CharField(max_length=100, default='',null=False, verbose_name='名称')
    # kpi = models.CharField(max_length=100, default='',null=False, verbose_name='kpi任务')


    class Meta:
        verbose_name = '部门管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name




